package com.my.juc.callable;

import java.util.concurrent.CountDownLatch;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/5 20:49
 */

public class CountDownLatchDemo {
    
    

    // 6个同学陆续离开教室之后，锁门
    public static void main(String[] args) throws InterruptedException {
        
        // 创建CountDownLatch对象，设置初始值
        // 相当于临界资源
        // 不为0则会一直等待 相当于计数器
        CountDownLatch countDownLatch = new CountDownLatch(6);
        
        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "号同学离开教室");
                // 计数 - 1
                countDownLatch.countDown();
                System.out.println("countDownLatch.getCount() = " + countDownLatch.getCount());
            }, i + "").start();
        }
        
        // 等待
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + "锁门跑路");

    }
    
}
